const app = new Vue({
    el: '#supBox',
    data: function () {
        return {
            isShow: 1,
            form1: {
                traceableCode: ''
            },
            form2: {
                manName: '',
                drugName: ''
            },
            codeTable: [],
            form3: {},
            manTable: [],
            traTable: [],
            selTable: [],
            conTable: [],
            userTable: [],
            form4: {
                username: '',
                phoneNumber: '',
                supName: '',
                address: '',
                manager: ''
            },
            pwdForm: {
                oldPwd: '',
                newPwd: ''
            },
            phoForm: {
                param: ''
            },
            pwdOpen: false,
            phoOpen: false,
            formLabelWidth: '120px'
        }
    },
    methods: {
        change1: function () {
            this.isShow = 1;
        },
        change2: function () {
            this.isShow = 2;
        },
        change3: function () {
            this.isShow = 3;
            // 获取所有用户信息
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.get('/user/getUser').then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.userTable = res.data.data;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        change4: function () {
            this.isShow = 4;
            // 获取当前用户信息
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.get('/info/getSup', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form3 = res.data.data;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        logout: function () {
            let token = window.localStorage.getItem('userToken');
            axios.get('/logout', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        location.href = '../../index.html';
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        trace: function () {
            if (this.form1.traceableCode === '') {
                this.$message.warning('请输入药品溯源编码！');
                return;
            }

            let that = this;
            that.manTable = [];
            that.traTable = [];
            that.selTable = [];
            that.conTable = [];
            //溯源生产数据
            axios.post('/search/manTable', {param: that.form1.traceableCode}).then(
                function (res) {
                    console.log(res);
                    let code = res.data.code;
                    if (code === 200) {
                        that.manTable.push(res.data.data);
                    } else if (code === 1001) {
                        console.log(that.form1);
                    }
                }, function (err) {
                    console.log(err);
                }
            )

            //溯源运输数据
            axios.post('/search/traTable', {param: that.form1.traceableCode}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.traTable = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.form1);
                    }
                }, function (err) {
                    console.log(err);
                }
            )

            //溯源销售商购买数据
            axios.post('/search/selTable', {param: that.form1.traceableCode}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.selTable = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.form1);
                    }
                }, function (err) {
                    console.log(err);
                }
            )

            //溯源消费者数据
            axios.post('/search/conTable', {param: that.form1.traceableCode}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.conTable = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.form1);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        cleanForm: function () {
            this.form2 = {
                manName: '',
                drugName: ''
            }
        },
        searchCode: function () {
            let that = this;
            axios.post('/search/searchCode', {
                first: that.form2.manName,
                second: that.form2.drugName
            }).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.codeTable = res.data.data;
                        that.cleanForm();
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        enable: function (index, data) {
            // 启用
            let username = data.username;
            let that = this;
            axios.post('/user/enable', {param: username}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.$message.success('启用成功');
                    } else if (code === 1001) {
                        console.log(username);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        disable: function (index, data) {
            // 禁用
            let username = data.username;
            let that = this;
            axios.post('/user/disable', {param: username}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.$message.success('禁用成功');
                    } else if (code === 1001) {
                        console.log(username);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        cancelChangePwd: function () {
            this.pwdForm = {
                oldPwd: '',
                newPwd: ''
            }
            this.pwdOpen = false;
        },
        changePwd: function () {
            //修改密码
            if (this.pwdForm.oldPwd === '' || this.pwdForm.newPwd === '') {
                this.$message.warning('密码不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changePwd', that.pwdForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功，请重新登录！');
                        location.href = '../../index.html';
                    } else if (code === 1007) {
                        that.$message.error('原始密码输入错误，请重试！');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.pwdOpen = false;
        },
        cancelChangePho: function () {
            this.phoForm.newPhone = '';
            this.phoOpen = false;
        },
        changePho: function () {
            //修改手机号码
            if (this.phoForm.param === '') {
                this.$message.warning('内容不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changePho', that.phoForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功！');
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.phoOpen = false;
        }
    },
})